package com.tracker;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tracker.util.MasterInformation;
import com.tracker.util.TrackerConnection;



public class Iom extends HttpServlet {
	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		TrackerConnection traclerConnection = new TrackerConnection();
		HttpSession session = request.getSession();
		String iomNo = request.getParameter("iomNo");
		String date = request.getParameter("date");
		String time = request.getParameter("time");
		String from = request.getParameter("from");
		String to = request.getParameter("to");
		String slno = request.getParameter("slno");
		String origin = "HYD";//request.getParameter("origin");		
		String cnnum = request.getParameter("cnnum");
		String remarks = request.getParameter("remarks");
		String operator = "HYD";request.getParameter("operator");
		
		String type = request.getParameter("type");
		String tableName = null;
		Connection conn = null;
		try{
			conn = traclerConnection.getConnection();
			tableName = "tracker.iom" + type;
			String insertQuery = "insert into " + tableName 
					+ "(iomno,iomdt,iomtm,frombrn,tobrn,sno,origin,consignnum,remarks,opr)" +
					" values(?,?,?,?,?,?,?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(insertQuery);
			pstmt.setString(1, iomNo);
			pstmt.setDate(2, new Date(Calendar.getInstance().getTimeInMillis()));
			pstmt.setString(3, "1011");
			pstmt.setString(4, MasterInformation.getBrancheCodeByDesc().get(from));
			pstmt.setString(5, MasterInformation.getBrancheCodeByDesc().get(to));
			pstmt.setString(6, slno);
			pstmt.setString(7,  MasterInformation.getBrancheCodeByDesc().get(session.getAttribute("userBranch")));
			pstmt.setString(8, cnnum);
			pstmt.setString(9, remarks);
			pstmt.setString(10, operator);			
			pstmt.execute();
			request.getRequestDispatcher("/menu.jsp").forward(request, response);
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

}
